package Frame.shopSet;

import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import VWorldGameStudio.*;
import lombok.Setter;
import Function.FCMutilPlayCache;

import java.util.ArrayList;
import java.util.Vector;

import static FCRendering.FCRMaster.*;

//保管物品和拿取
public class FunctionSaveAndTakeShop extends AShopFuntion {
    private Vector<FixTank> needServiceNames = new Vector<>();

    private int showFristIndex = 0;
    @Setter
    private int endI = 0;

    private int witchPage = 0;//0 是主页，1是保管页， 10是详情保管谁的背包物品页  2是取页， 20是详情取页
    FixTank selectFixTank = null;

    private ArrayList<String> itemNames = new ArrayList<>();

    private String selectItemId = null;
    private int selectIndex = 0;
    public FunctionSaveAndTakeShop() {
        buildShopWindow();
    }

    public void buildShopWindow() {
        needServiceNames.add(new FixTank(player1, player1.getName()));
        FCTank tank = tankLib.getTankOfID(player1.getPlayertank1(), player1.getPlayertankHostId());
        if(tank != null){
            needServiceNames.add(new FixTank(tank, langLib.getNameOfId(tank.getName())));
        }
        for(FCRPlayerLink link: FCMutilPlayCache.getTeamMembers()){
            if(link.curPlayerHostUID.equals(gameSave.HOST_UID)){
                FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(gameSave.HOST_UID, link.curPlayerName);
                needServiceNames.add(new FixTank(p, p.getChName()));
                tank = tankLib.getTankOfID(p.getPlayertank1(),p.getPlayertankHostId());
                if(tank != null){
                    needServiceNames.add(new FixTank(tank, langLib.getNameOfId(tank.getName())));
                }
            }
        }

        for (FCPropulsion fc : fcv.getPropulsions()) {
            if(fc.getPropulsionHostId().equals(gameSave.HOST_UID) && fc.getPropulsionType().equals("tank")){
                needServiceNames.add(new FixTank((FCTank) fc, langLib.getNameOfId(((FCTank) fc).getName())));
            }
        }
        resetUI();
    }

    private void resetUI() {
        FCRValues.resetUnifySayUIEntity();
        if(witchPage == 0){
            FCRValues.leftWindowUI[0].leftText = "保管业务";
            FCRValues.leftWindowUI[1].leftText = "提取业务";

        }else if(witchPage == 1){
            FCRValues.leftWindowUI[0].leftText = "保管那个背包的东西？";
            int m = 1;
            for (int i = showFristIndex; i < endI; i++) {
                FixTank fixTank = needServiceNames.get(i);
                FCRValues.leftWindowUI[m].leftText = fixTank.text;
                m++;
            }
        }else if(witchPage == 10){
            FCRValues.leftWindowUI[0].leftText = "保管"+selectFixTank.text+"的物品";
            int m = 1;
            for (int i = showFristIndex; i < endI; i++) {
                String iName = itemNames.get(i);
                FCItem item = FCRMaster.itemLib.getItemOfID(iName);
                FCRValues.leftWindowUI[m].leftText = item != null?item.getChName():"空位";
                m++;
            }
            FCRValues.rightWindowUI[5].rightText = "保管";
            FCRValues.rightWindowUI[3].rightText = "上一页";
            FCRValues.rightWindowUI[4].rightText = "下一页";
        }else if(witchPage == 2){
            FCRValues.leftWindowUI[0].leftText = "提取到那个背包？";
            int m = 1;
            for (int i = showFristIndex; i < endI; i++) {
                FixTank fixTank = needServiceNames.get(i);
                FCRValues.leftWindowUI[m].leftText = fixTank.text;
                m++;
            }
        }else if(witchPage == 20){
            FCRValues.leftWindowUI[0].leftText = "提取到"+selectFixTank.text+"的背包";
            int m = 1;
            for (int i = showFristIndex; i < endI; i++) {
                String iName = itemNames.get(i);
                FCItem item = FCRMaster.itemLib.getItemOfID(iName);
                FCRValues.leftWindowUI[m].leftText = item != null?item.getChName():"空位";
                m++;
            }
            FCRValues.rightWindowUI[3].rightText = "上一页";
            FCRValues.rightWindowUI[4].rightText = "下一页";
            FCRValues.rightWindowUI[5].rightText = "提取";
        }

        if(witchPage == 0 || witchPage == 1 || witchPage == 2) {
            FCRValues.rightWindowUI[6].rightText = "退出";
        }else {
            FCRValues.rightWindowUI[6].rightText = "返回";
        }
    }

    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        if(entity.leftText.length() > 1) {
            if(witchPage == 0){
                witchPage = i+1;
                showFristIndex = 0;
                setEndI(Math.min(needServiceNames.size(), showFristIndex + 6));
                resetUI();
            }else if(witchPage == 1){
                selectFixTank = needServiceNames.get(showFristIndex+i-1);
                witchPage = 10;
                showFristIndex = 0;
                getItemList();
                setEndI(Math.min(itemNames.size(), showFristIndex + 6));
                resetUI();
            }else if(witchPage == 10){
                selectIndex = showFristIndex+i-1;
                selectItemId = itemNames.get(selectIndex);
                FCItem item = FCRMaster.itemLib.getItemOfID(selectItemId);
                if(item != null) {
                    FCRValues.rightWindowUI[0].leftText = "将要保管" + item.getChName();
                }
            }else if(witchPage == 2){
                FixTank tmp = needServiceNames.get(showFristIndex+i-1);
                if(tmp.playerInfo != null){
                    for(int n = 0; n<10;n++){
                        String itemn = tmp.playerInfo.getKnapsack()[n];
                        if(itemn == null || itemn.equals("null")){
                            break;
                        }
                        if(n == 9){
                            FCRValues.rightWindowUI[0].leftText = tmp.playerInfo.getChName()+"背包没空间了";
                            return;
                        }
                    }
                }else if(tmp.tank != null){
                    for(int n = 0; n<10;n++){
                        String itemn = tmp.tank.getKnapsack()[n];
                        if(itemn == null || itemn.equals("null")){
                            break;
                        }
                        if(n == 9){
                            FCRValues.rightWindowUI[0].leftText = langLib.getNameOfId(tmp.tank.getName())+"背包没空间了";
                            return;
                        }
                    }
                }
                selectFixTank = tmp;
                witchPage = 20;
                showFristIndex = 0;
                getItemList();
                setEndI(Math.min(itemNames.size(), showFristIndex + 6));
                resetUI();
            }else if(witchPage == 20){
                selectIndex = showFristIndex+i-1;
                selectItemId = itemNames.get(selectIndex);
                FCItem item = FCRMaster.itemLib.getItemOfID(selectItemId);
                if(item != null) {
                    FCRValues.rightWindowUI[0].leftText = "将要提取" + item.getChName();
                }
            }
        }
    }

    private void getItemList(){
        itemNames.clear();
        if(selectFixTank != null){
            if(witchPage == 10) {
                if (selectFixTank.tank != null) {
                    //获取坦克的背包道具
                    for (int i = 0; i < 10; i++) {
                        itemNames.add(selectFixTank.tank.getKnapsack()[i]);
                    }
                } else if (selectFixTank.playerInfo != null) {
                    //获取人类的背包道具
                    for (int i = 0; i < 10; i++) {
                        itemNames.add(selectFixTank.playerInfo.getKnapsack()[i]);
                    }
                }
            }else if(witchPage == 20){
                itemNames.addAll(gameSave.getFcBankList());
            }
        }
    }

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        switch (i){
            case 3:
                if(witchPage != 0) {
                    if (showFristIndex >= 6) {
                        showFristIndex -= 6;
                        if (witchPage == 10 || witchPage == 20) {
                            setEndI(Math.min(itemNames.size(), showFristIndex + 6));
                        } else {
                            setEndI(Math.min(needServiceNames.size(), showFristIndex + 6));
                        }
                        resetUI();
                    }
                }
                break;
            case 4:
                if(witchPage != 0) {
                    if (endI < itemNames.size()) {
                        showFristIndex += 6;
                        if(witchPage == 10||witchPage == 20) {
                            setEndI(Math.min(itemNames.size(), showFristIndex + 6));
                        }else {
                            setEndI(Math.min(needServiceNames.size(), showFristIndex + 6));
                        }
                        resetUI();
                    }
                }
                break;
            case 5:
                if(selectItemId == null){
                    return;
                }
                if(witchPage == 10){
                    FCItem item = FCRMaster.itemLib.getItemOfID(selectItemId);
                    if(item != null) {
                        gameSave.getFcBankList().add(selectItemId);
                        if(selectFixTank.tank != null){
                            selectFixTank.tank.insertItemToKnapsack(selectIndex, null);
                            selectFixTank.tank.reInitShuxing();
                        }else if(selectFixTank.playerInfo != null){
                            selectFixTank.playerInfo.insertItemToKnapsack(selectIndex, null);
                        }
                        selectItemId = null;
                        getItemList();
                        setEndI(Math.min(itemNames.size(), showFristIndex + 6));
                        resetUI();
                    }
                }else if(witchPage == 20){
                    FCItem item = FCRMaster.itemLib.getItemOfID(selectItemId);
                    if(item != null) {
                        if(selectFixTank.tank != null){
                            gameSave.getFcBankList().remove(selectIndex);
                            selectFixTank.tank.addItemToKnapsack(selectItemId);
                            selectFixTank.tank.reInitShuxing();
                        }else if(selectFixTank.playerInfo != null){
                            if(item.getCatalog().equals("tank")){
                                FCRValues.rightWindowUI[0].leftText = "坦克道具不能提取到人类背包中!";
                                break;
                            }
                            selectFixTank.playerInfo.addItemToKnapsack(selectItemId);
                            gameSave.getFcBankList().remove(selectIndex);
                        }
                        selectItemId = null;
                        getItemList();
                        setEndI(Math.min(itemNames.size(), showFristIndex + 6));
                        resetUI();
                    }
                }
                break;
            case 6:
                if(witchPage == 0 || witchPage == 1 || witchPage == 2) {
                    fcv.setOpenSayShop(false);
                    FCRMaster.fcv.setOpenSay(false);
                    FCRMaster.fcv.setSayNpcName(null);
                    FCRMaster.fcv.setSayNpcTextNum(0);
                }else if(witchPage == 10){
                    witchPage = 1;
                    showFristIndex = 0;
                    selectItemId = null;
                    setEndI(Math.min(needServiceNames.size(), showFristIndex + 6));
                    resetUI();
                }else if(witchPage == 20){
                    witchPage = 2;
                    showFristIndex = 0;
                    selectItemId = null;
                    setEndI(Math.min(needServiceNames.size(), showFristIndex + 6));
                    resetUI();
                }
                break;
        }
    }
}
